package com.rcadmin.busi.web.action;

import java.util.HashMap;
import java.util.Map;

import javax.annotation.Resource;

import org.apache.commons.lang.StringUtils;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.servlet.ModelAndView;

import com.rcadmin.busi.web.ddl.WebBlock;
import com.rcadmin.busi.web.ddl.WebTemplateGroupkey;
import com.rcadmin.busi.web.dto.WebBlockDto;
import com.rcadmin.busi.web.service.BlockService;
import com.rcadmin.busi.web.service.GroupKeyService;
import com.rcadmin.busi.web.service.TemplateService;
import com.rcadmin.common.contant.SysParamCode;
import com.rcadmin.common.exception.BusiException;
import com.rcadmin.core.app.session.SessionException;
import com.rcadmin.core.jpa.util.PageBean;
import com.rcadmin.core.web.BaseAction;

/**
 * 网站栏目展示类
 * 
 * @author tanson lam
 * @creation 2015年1月10日
 */
@Controller
public class BlockController extends BaseAction {

	private static final String LIST_PAGE = "/web/block/blocklist";
	private static final String EDIT_FORM = "/web/block/blockform";
	@Resource
	private GroupKeyService groupKeyService;
	@Resource
	private BlockService blockService;
	@Resource
	private TemplateService templateService;

	/**
	 * 查詢列表
	 * 
	 * @throws SessionException
	 */
	@GetMapping("/block/show.op")
	public ModelAndView show() {
		Map<String, Object> dataModel = new HashMap<String, Object>();
		Integer groupKeyId = getIntegerParameter("_QUERY_n_eq_group_key_id");
		String condition = "user_id=" + getLoginUserId() + bulidConditionSql();
		PageBean<WebBlockDto> pageBean = blockService.pageQuery(condition,
				bulidOrderBySql(), getPageSize(), getPage());
		dataModel.put("groupKeyCode",
				groupKeyService.getGroupParamById(groupKeyId));
		dataModel.put("iseffCode",
				sysParamService.getSysParamByRoot(SysParamCode.ISEFF));
		pageBean.setPageUrl(getPaginationUrl("/block/show.op"));
		dataModel.put("pageBean", pageBean);
		return render(LIST_PAGE, dataModel);
	}

	/**
	 * 显示编辑表单
	 */
	@GetMapping("/block/showEditForm.op")
	public ModelAndView showEditForm() {
		Map<String, Object> dataModel = new HashMap<String, Object>();
		String id = request.getParameter("id");
		Integer groupKeyId = getIntegerParameter("groupKeyId");
		if (!StringUtils.isEmpty(id)) {
			WebBlock webBlock = blockService.findOne(getLoginUserId(),
					Integer.valueOf(id));
			dataModel.put("bean", webBlock);
		}
		WebTemplateGroupkey webTemplateGroupkey = groupKeyService.findOne(
				getLoginUserId(), groupKeyId);
		if (webTemplateGroupkey == null) {
			throw new BusiException("没有对应的分类信息");
		}
		dataModel
				.put("blockLinkType", sysParamService
						.getSysParamByRoot(SysParamCode.BLOCK_LINK_TYPE));
		dataModel.put("iseffCode",
				sysParamService.getSysParamByRoot(SysParamCode.ISEFF));
		dataModel.put("groupKeyCode",
				groupKeyService.getGroupParamById(groupKeyId));
		dataModel.put("templateIdCode", templateService.getResTemplate(
				getLoginUserId(), webTemplateGroupkey.getTemplatePackageId()));
		return render(EDIT_FORM, dataModel);
	}

}
